package com.rabbit.library.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.rabbit.library.R;
import com.rabbit.library.widget.LinePathView;

import java.io.File;

/**
 * 电子签名
 */
public class ElectronSignActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnDetermine;
    private TextView mTitileName;
    private TextView mTitileRightName;
    private String  signType;
    private String shopeName;
    LinePathView lpCanvas;
    public static String IMAGE_PATH;
    private int  position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_electron_sign);
        if (ContextCompat.checkSelfPermission(ElectronSignActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(ElectronSignActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1);
        }
        initView();

    }

    private void initView() {
        Bundle bundleParms = getIntent().getExtras();
        if (bundleParms != null) {
            signType = bundleParms.getString("signType");
            shopeName = bundleParms.getString("shopeName");
            position = bundleParms.getInt("position");
            /**
             * 图片保存路径
             */
            IMAGE_PATH = File.separator + "yunluqujian" + File.separator + "images";
        }else{
            /**
             * 图片保存路径
             */
            IMAGE_PATH = File.separator + "yunlu" + File.separator + "images2";

        }
        btnDetermine = findViewById(R.id.btn_determine);
        mTitileName = findViewById(R.id.tv_title_name);
        mTitileRightName = findViewById(R.id.tv_right_title_name);
        lpCanvas = findViewById(R.id.lp_canvas);
        mTitileRightName.setVisibility(View.VISIBLE);
        mTitileName.setText("电子签名");
        mTitileRightName.setText("重输");
        btnDetermine.setOnClickListener(this);
        mTitileRightName.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_determine) {
            if ("signPickImg".equals(signType)) {
                if (shopeName.isEmpty()) {
                    Toast.makeText(this,"该单号对应商家名称为空，不能进行签名",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (lpCanvas.getTouched()) {
                    Intent intent = new Intent();
                    try {
                        //   内置存储文件目录
                        String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + IMAGE_PATH;        //内存卡的图片保存路径
                        File fileDir = new File(savePath);
                        if (!fileDir.exists()) {
                            fileDir.mkdirs();
                        }
                        String times = "01" + (int) ((Math.random() * 9 + 1) * 10000000) + System.currentTimeMillis() + "";
                        String imgId = times + "IMGCODE168";
                        String siteCode =  "siteCode";
                        String userCode =  "userCode";
                        String fileName = imgId + "-" + "Pickup_Signature_Picture" + "-" + siteCode + "-" + userCode + "-" + times + ".jpg";

                        final String path = savePath + File.separator + fileName;
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    lpCanvas.save(path, true, 10);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                        intent.putExtra("imageUrl", path);
                        intent.putExtra("position", position);
                        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        intent.putExtra("imageUrl", "");
                        intent.putExtra("position", position);
                        Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    Toast.makeText(this,"请先签名后保存",Toast.LENGTH_SHORT).show();
                }
            } else {
                if (lpCanvas.getTouched()) {
                    Intent intent = new Intent();
                    try {
                        //   内置存储文件目录
                        String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + IMAGE_PATH;        //内存卡的图片保存路径
                        File fileDir = new File(savePath);
                        if (!fileDir.exists()) {
                            fileDir.mkdirs();
                        }
                        String mUserCode = "mUserCode";
                        String fileName = mUserCode + System.currentTimeMillis() + ".jpg";
                        final String path = savePath + File.separator + fileName;
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    lpCanvas.save(path, true, 10);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();

                        intent.putExtra("imageUrl", path);
                        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        intent.putExtra("imageUrl", "");
                        Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    Toast.makeText(this,"请先签名后保存",Toast.LENGTH_SHORT).show();
                }
            }
        } else if (id == R.id.tv_right_title_name) {
            lpCanvas.clean();
        }
    }

}
